
//
// 110605 - AH - Created.
//

package org.soekrat.gui.swing.chat;

import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.IOException;
import java.net.URL;

import javax.swing.AbstractAction;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.SwingUtilities;

import org.soekrat.engine.Soekrat2Engine;
import org.soekrat.engine.io.XmlFileSoekratDataBase;
import org.soekrat.environment.conversationbase.SoekratDatabase;
import org.soekrat.gui.swing.ErrorDialog;
import org.soekrat.gui.swing.SoekratLaunchDialog;
import org.soekrat.gui.swing.studio.Studio;

/**
 * <p>
 *   Base class for desktop Soekrat applications.
 * </p>
 * <p>
 *   Main is program that launches Soekrat on the desktop.
 * </p>
 * @author Arne Halvorsen (AH)
 * @author Laetitia Le Chatton (LLC) 
 */
@SuppressWarnings("serial")
public abstract class DesktopSoekrat extends JFrame
{
  private final Soekrat2Engine m_engine;
  
  /**
   * <p>
   *   Constructor.
   * </p>
   * @param engine {@link Soekrat2Engine} interfaced.
   * @param title  Frame's title.
   */
  protected DesktopSoekrat(Soekrat2Engine engine, String title)
  {
    if (engine==null)
    {
      throw new NullPointerException("engine");
    }
    if (title==null)
    {
      throw new NullPointerException("title");
    }
    
    URL url=ClassLoader.getSystemResource("org/soekrat/gui/swing/srat.png");
    if (url!=null)
    {
      Image image=Toolkit.getDefaultToolkit().createImage(url);
      
      setIconImage(image);
    }
    
    m_engine=engine;
    
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    addWindowListener
    (
      new WindowAdapter() 
      {
        public void windowClosing(WindowEvent e) 
        {
          try 
          { 
            m_engine.save();
          }
          catch (IOException iox){ iox.printStackTrace(); System.exit(-1); }
        }                
      }
    );
    
    setTitle(title);
    
    buildGui();
  }
  
  /**
   * <p>
   *   Override to build GUI.
   * </p>
   */
  protected abstract void buildGui();
  
  /**
   * <p>
   *   Gets
   *   {@link Soekrat2Engine} interfaced.
   * </p>
   * @return {@code SoekratEngine}.
   */
  public final Soekrat2Engine getEngine(){ return m_engine; }
  
  protected final JMenu createFileMenu()
  {
    JMenu retVal=new JMenu("File");
    
    retVal.add
    (
      new AbstractAction("Save") 
      {
        public void actionPerformed(ActionEvent e) 
        {
          try
          {
            Soekrat2Engine engine=getEngine();
            engine.save();
          }
          catch (IOException iox)
          {
            iox.printStackTrace();
          }
        }
      }
    );
    
    retVal.add
    (
      new AbstractAction("Exit") 
      {
        public void actionPerformed(ActionEvent e) 
        {
          try
          {
            DesktopSoekrat.this.setVisible(false);
            DesktopSoekrat.this.dispose();
            
            Soekrat2Engine engine=getEngine();
          
            engine.save();
          }
          catch (IOException iox)
          {
            iox.printStackTrace();
          }
          
          System.exit(0);
        }
      }
    );
    
    return retVal;
  }
  
  /**
   * <p>
   *   Launches Soekrat Rat Studio.
   * </p>
   * @param args Command line arguments, not used. 
   */
  public static void main(String[] args) 
  {
    SwingUtilities.invokeLater
    (
      new Runnable() 
      {
        public void run() 
        {
          try
          {
            SoekratLaunchDialog sld=new SoekratLaunchDialog();
            
            if (sld.launch())
            {
              File dir=sld.getDir();
              SoekratDatabase db=(dir==null) ? new XmlFileSoekratDataBase()
                                             : new XmlFileSoekratDataBase(dir);
              Soekrat2Engine engine=new Soekrat2Engine(db);
          
              if (sld.launchStudio())
              {
                Studio studio=new Studio(engine);
                studio.setVisible(true);
              }
              else
              {
                Chat chat=new Chat(engine);
                chat.setSize(800, 600);
                chat.setVisible(true);
              }
            }
          }
          catch (IOException iox)
          {
            ErrorDialog.show(iox);            
          }
        }
      }
    );
  }
  
}
